package com.tycmc.signelectroniccontracts.tools.other;

import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gaoyong on 2018/3/17.
 */

public class TextShow {
    public static boolean isEmpty(String text) {
        if (text == null) {
            return true;
        }
        return TextUtils.isEmpty(text);
    }

    public static void setTextNeed(TextView textView) {
        String trim = textView.getText().toString().trim();
        String str = "<font color='red'>*</font>" + trim;
        textView.setText(Html.fromHtml(str));
    }

    public static void setTextnotNeed(TextView textView) {
        String trim = textView.getText().toString().trim();
        String str = "\u2000" + trim;
        textView.setText(str);
    }

    public static String valueOf(Object o) {
        return valueOf(o, "");
    }

    public static String valueOf(Object o, String defalut) {
        if (o != null && !String.valueOf(o).equals("null")) {
            return String.valueOf(o);
        } else {
            return defalut;
        }
    }

    public static void deleteChar(EditText editText) {
        int index = editText.getSelectionStart();
        Editable editable = editText.getText();
        editable.delete(index - 1, index);
    }

    public static void addChar(EditText editText, String data) {
        int index = editText.getSelectionStart();
        Editable editable = editText.getText();
        editable.insert(index, data);
    }

    public static String getDoubMoney(Object o) {
        if (o instanceof Double) {
            Double aDouble = (Double) o;
            DecimalFormat df = new DecimalFormat("#0.00");
            return df.format(aDouble);
        } else {
            try {
                Double aDouble = Double.valueOf(o.toString());
                DecimalFormat df = new DecimalFormat("#0.00");
                return df.format(aDouble);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return o.toString();

    }

    /**
     * 千分位显示逗号
     *
     * @param money
     * @return
     */
    public static String setMoney(Double money) {
        DecimalFormat df = new DecimalFormat("#0.00");
        String valueOf = String.valueOf(df.format(money));
        int i = valueOf.length() - 3;
        while (i - 3 > 0) {
            i = i - 3;
            valueOf = valueOf.substring(0, i) + ", " + valueOf.substring(i);
        }
        return valueOf;
    }

    public static String setMoney(String str) {
        try {
            Double money = Double.valueOf(str);
            return setMoney(money);
        } catch (Exception e) {
            return str;
        }

    }


    /**
     * 只判断汉字
     *
     * @param str
     * @return
     */
    public static int getChineseCountWord(String str) {
        int count = 0;
        Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher m = p.matcher(str);
        while (m.find()) {
            for (int i = 0; i <= m.groupCount(); i++) {
                count = count + 1;
            }
        }
        return str.length() + count;
    }

    /**
     * 中文标点
     *
     * @param str
     * @return
     */
    public static int getChineseCount(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (isChinesePunctuation(str.charAt(i)) || isChineseByBlock(str.charAt(i))) {
                count = count + 1;
            }
        }
        return str.length() + count;
    }

    //使用UnicodeBlock方法判断
    public static boolean isChineseByBlock(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isChinesePunctuation(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS
                || ub == Character.UnicodeBlock.VERTICAL_FORMS) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isMoney(String str) {
        try {
            Double aDouble = Double.valueOf(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
